﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Serialization;
using System.Xml;
using Infrastructure;
using Inspection.Views;
using MaterialDesignThemes.Wpf;
using System.Collections.ObjectModel;
using System.IO;
using Xceed.Wpf.Toolkit;

using System.Dynamic;
using System.Windows.Controls.Primitives;
using Microsoft.VisualBasic;
using System.Windows.Threading;
using System.Threading;
using Color = System.Drawing.Color;
using Inspection.Helper;
using System.Windows.Forms;
using System.Diagnostics;
using UserManager;
using Inspection.Models;
using System.Reflection;

namespace Inspection
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : System.Windows.Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
            AddCommand();
            DataContext = model;
             parame = SysInitialParame.Load();
            if(null== parame) parame=new SysInitialParame();
            model.SysInitial = parame;
            if (!string.IsNullOrEmpty(parame.ProjectFile))
            {
                model.ExcuteRecipe = ProjectRecipe.LoadProject(parame.ProjectFile);
            }
            GlobalLogMessageEvent.Publish(new MessageData(MessageType.Info,"软件启动"));
            dispatcher.Interval = new TimeSpan(0,0,2);
            dispatcher.Tick += Dispatcher_Tick;
            dispatcher.Start();
            AccountHelper.UserTypeChanged += AccountHelper_UserTypeChanged;
            Closed += MainWindow_Closed;
        }

        private void MainWindow_Closed(object sender, EventArgs e)
        {
            parame.Save();
        }
        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged([CallerMemberName] string PropertyName = null)
        {
            if (null != PropertyChanged)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
            }
        }
        #endregion INotifyPropertyChanged

        private void AccountHelper_UserTypeChanged(object sender, UserManager.UserTypes e)
        {
            model.UserType = e;
        }

        private void Dispatcher_Tick(object sender, EventArgs e)
        {
            model.TCPConnected = GlobalParame.MainTCPClient != null && GlobalParame.MainTCPClient.IsConnected;
        }

        private readonly MainModel model = new MainModel();
        private readonly SysInitialParame parame;
        private readonly DispatcherTimer dispatcher = new DispatcherTimer(DispatcherPriority.Background);
    }
}
